package aos.voting.entity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import aos.voting.exception.AosException;

public class GlobalVariables {

	public static int seed = 123456;
	public static int nodeId = 0;
	public static int objectNum = 4;
	public static double awaitingGrant = 20;
	public static double holdTime = 0.1;
	public static double minSleep = 5;
	public static double maxSleep = 10;
	public static int loglevel = 0xffffffff;
	public static String logfile = null;
	public static int logAppend = 1;
	public static String input = "input.txt";

	public static void initialize(String configsFile) throws AosException {
		Properties properties = new Properties();
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(configsFile);
		} catch (FileNotFoundException e) {
			System.out.println("file \"" + configsFile + "\" not found");
			e.printStackTrace();
			throw new AosException(e.getMessage(), e);
		}
		System.out.println("configsFile open success");
		try {
			properties.load(fis);
		} catch (IOException e) {
			System.out.println("configsFile \"" + configsFile
					+ "\" load failed");
			e.printStackTrace();
			throw new AosException(e.getMessage(), e);
		}
		System.out.println("configsFile load success");

		if (properties.getProperty("seed") != null) {
			seed = Integer.parseInt(properties.getProperty("seed"));
			System.out.println("seed is: " + seed);
		} else {
			System.out.println("seed is: " + seed + " (default)");
		}

		if (properties.getProperty("nodeid") != null) {
			nodeId = Integer.parseInt(properties.getProperty("nodeid"));
			System.out.println("nodeid is: " + nodeId);
		} else {
			System.out.println("nodeid is: " + nodeId + " (default)");
		}

		if (properties.getProperty("objectnum") != null) {
			objectNum = Integer.parseInt(properties.getProperty("objectnum"));
			System.out.println("objectnum is: " + objectNum);
		} else {
			System.out.println("objectnum is: " + objectNum + " (default)");
		}
		if (properties.getProperty("logappend") != null) {
			logAppend = Integer.parseInt(properties.getProperty("logappend"));
			System.out.println("logappend is: " + logAppend);
		} else {
			System.out.println("logappend is: " + logAppend + " (default)");
		}

		if (properties.getProperty("awaitinggrant") != null) {
			awaitingGrant = Double.parseDouble(properties
					.getProperty("awaitinggrant"));
			System.out.println("awaitinggrant is: " + awaitingGrant);
		} else {
			System.out.println("awaitinggrant is: " + awaitingGrant
					+ " (default)");
		}

		if (properties.getProperty("holdtime") != null) {
			holdTime = Double.parseDouble(properties.getProperty("holdtime"));
			System.out.println("holdtime is: " + holdTime);
		} else {
			System.out.println("holdtime is: " + holdTime + " (default)");
		}

		if (properties.getProperty("minsleep") != null) {
			minSleep = Double.parseDouble(properties.getProperty("minsleep"));
			System.out.println("minsleep is: " + minSleep);
		} else {
			System.out.println("minsleep is: " + minSleep + " (default)");
		}

		if (properties.getProperty("maxsleep") != null) {
			maxSleep = Double.parseDouble(properties.getProperty("maxsleep"));
			System.out.println("maxsleep is: " + maxSleep);
		} else {
			System.out.println("maxsleep is: " + maxSleep + " (default)");
		}

		if (properties.getProperty("loglevel") != null) {
			loglevel = Integer.parseInt(properties.getProperty("loglevel"));
			System.out.println("loglevel is: " + Integer.toHexString(loglevel));
		} else {
			System.out.println("loglevel is: " + Integer.toHexString(loglevel)
					+ " (default)");
		}

		if (properties.getProperty("logfile") != null) {
			logfile = properties.getProperty("logfile");
			System.out.println("logfile is: " + logfile);
		} else {
			System.out.println("logfile is: " + logfile + " (default)");
		}

		if (properties.getProperty("input") != null) {
			input = properties.getProperty("input");
			System.out.println("input is: " + input);
		} else {
			System.out.println("input is: " + input + " (default)");
		}
	}
}
